package sy.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.db.mysql.dao.TroleDao;
import sy.db.mysql.dao.TroleTresourceDao;
import sy.db.mysql.dao.TuserTroleDao;
import sy.db.mysql.model.Trole;
import sy.db.mysql.model.TroleTresource;
import sy.db.mysql.model.TuserTrole;
import sy.pageModel.SessionInfo;
import sy.pageModel.Tree;
import sy.services.RoleServiceI;

@Service
public class RoleServiceImpl implements RoleServiceI {
	
	@Autowired
	TuserTroleDao tuserTroleDao;
	@Autowired
	TroleDao troleDao;
	@Autowired
	TroleTresourceDao  troleTresourceDao;
	@Override
	public void add(Trole role, SessionInfo sessionInfo) {
		this.troleDao.insertSelective(role);
		TuserTrole userTrole = new TuserTrole();
		// 刚刚添加的角色，赋予给当前的用户
		userTrole.setID(UUID.randomUUID().toString());
		userTrole.setTROLEIDSTR(role.getID());
		userTrole.setTUSERIDSTR(sessionInfo.getId());
		this.tuserTroleDao.insert(userTrole );
		
	}

	@Override
	public Map get(String id) {
		return this.troleDao.selectByPrimaryKey(id);
	}

	@Override
	public void edit(Trole role) {
		this.troleDao.updateByPrimaryKeySelective(role);
		
	}

	@Override
	public List<Map> treeGrid(SessionInfo sessionInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("ID_EQ", "0");
		if (sessionInfo != null) {
			params.put("TUSERIDSTR_EQ", sessionInfo.getId());
		}
		List<Map> treeroot = this.troleDao.findRoleByUserIDByGrid(params);
		for(Map tree : treeroot){
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("PIDSTR_EQ", tree.get("ID").toString());
			params2.put("sort", "SEQNUM");
			params2.put("order", "ASC");
			if (sessionInfo != null) {
				params2.put("TUSERIDSTR_EQ", sessionInfo.getId());
			}
			List<Map> treechild = this.troleDao.findRoleByUserIDByGrid(params2);
			tree.put("children", treechild);
		}
		return treeroot;
	}
	
	@Override
	public void delete(String id) {
		this.troleDao.deleteByPrimaryKey(id);
	}



	@Override
	public List<Tree> allTree() {
		SessionInfo si = new SessionInfo();
		si.setId("0");
		return this.tree(si);
	}
	
	@Override
	public List<Tree> tree(SessionInfo sessionInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("ID_EQ", "0");
		if (sessionInfo != null) {
			params.put("TUSERIDSTR_EQ", sessionInfo.getId());
		}
		List<Tree> lt = new ArrayList<Tree>();
		List<Map> l = this.troleDao.findRoleByUserIDByGrid(params);
		if (l != null && l.size() > 0) {
			for (Map t : l) {
				Tree tree = new Tree();
				BeanUtils.copyProperties(t, tree);
				tree.setText(t.get("NAMESTR").toString());
				tree.setIconCls("status_online");
				List<Tree> children = new ArrayList<Tree>() ;
				tree.setChildren(children );
				Map<String, String> params2 = new HashMap<String, String>();
				params2.put("PIDSTR_EQ", t.get("ID").toString());
				if (sessionInfo != null) {
					params2.put("TUSERIDSTR_EQ", sessionInfo.getId());
				}
				List<Map> ll = this.troleDao.findRoleByUserIDByGrid(params2);
				for(Map map :ll){
					Tree tree2 = new Tree();
					tree2.setText(map.get("NAMESTR").toString());
					tree2.setIconCls("status_online");
					tree2.setId(map.get("ID").toString());
					tree2.setChecked(false);
					tree2.setPid(t.get("ID").toString());
					children.add(tree2);
				}
				lt.add(tree);
			}
		}
		return lt;
	}
	@Override
	public void grant(String  roleid , String resids) {
		troleTresourceDao.deleteByRoleID(roleid);
		String[] residarr = resids.split(",");
		for(String id : residarr){
			TroleTresource res = new TroleTresource();
			res.setTROLEIDSTR(roleid);
			res.setTRESOURCEIDSTR(id);
			res.setID(UUID.randomUUID().toString());
			troleTresourceDao.insertSelective(res);
		}
	}
	
}
